package com.orhon.smartcampus.modules.baseinfo.mutation;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.baseinfo.dao.WeChatTemplateDao;
import com.orhon.smartcampus.modules.baseinfo.entity.WeChatTemplate;
import com.orhon.smartcampus.modules.baseinfo.gqlInputModel.GQLWeChatTemplateInput;
import com.orhon.smartcampus.modules.baseinfo.inputMapper.WeChatTemplateInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class AddWeChatTemplateMutation implements DataFetcher {


    @Autowired
    WeChatTemplateDao weChatTemplateDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加微信消息推送模版
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        GQLWeChatTemplateInput input = WeChatTemplateInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        WeChatTemplate weChatTemplate = new WeChatTemplate();
        //查询该模版是否存在
        WeChatTemplate weChatTemplateUpdate = weChatTemplateDao.get(WeChatTemplate.class,input.getTemplateId());

        if(input.getTemplateId() != null){

            weChatTemplate.setTemplateId(input.getTemplateId());
        }
        if(input.getTemplateTitle() != null){

            weChatTemplate.setTemplateTitle(input.getTemplateTitle());
        }
        if(input.getTemplateType() != null){

            weChatTemplate.setTemplateType(input.getTemplateType());
        }
        if(input.getTemplatePhoto() != null){

            weChatTemplate.setTemplatePhoto(input.getTemplatePhoto());
        }
        try {
            if(weChatTemplateUpdate != null){
                weChatTemplateDao.update(weChatTemplate);
            }else{
                weChatTemplateDao.save(weChatTemplate);
            }
        } catch (Exception e) {
            return Result.error(500, "" + e);
        }
        return Result.ok();
    }
}

